Repository navigation
[rshapes] Fix half-pixel offsets in DrawRectangleLines() - #6430
Conversation
DrawRectangleLines()
|
@Ne0nWinds I'm afraid I prefer to avoid that fix. It requires adding more compute cost to every draw line just to align one pixel in certain cases, it could be GPU-dependant (in the past I got different results on Intel/Nvidia and even RPI Broadcom GPUs for pixel alignment on lines) and it adds an extra function that should be called on all line-drawing functions (actually, I'm removing the Despite that, I think it can be added as a commented block of code for users really facing this issue and to have a note on how it can be addressed if really crucial for their projects. |
|
@raysan5 I'm not sure if I was clear in the description. Some examples with videos/screenshots: 1. RotationRotating with rotation += 0.1f; // rotate the rectangle every single frame
rlPushMatrix();
rlTranslatef(screenWidth/2, screenHeight/2, 0.0f);
rlRotatef(rotation, 0, 0, 1);
DrawRectangleLines(-50, -50, 100, 100, GREEN);
rlPopMatrix();draw_rectangle_lines_rotate.mp42. DrawRectangle is inconsistent with DrawRectangleLines
BeginMode2D(camera); // offset = { 400, 225 }, zoom = 100.0f
DrawRectangle(-1, -1, 2, 2, DARKGRAY);
DrawRectangleLines(-1, -1, 2, 2, RED);
EndMode2D();
3. DrawRectangleRoundedLines with camera
// increment roundness by 0.005f every frame until it reaches 1.0f
BeginMode2D(camera); // offset = { 400, 225 }, zoom = 100.0f
DrawRectangleRounded((Rectangle){ -1, -1, 2, 2 }, roundness, 16, DARKGRAY);
DrawRectangleRoundedLines((Rectangle){ -1, -1, 2, 2 }, roundness, 16, RED);
EndMode2D();draw_rectangle_rounded_lines_offsets.mp4My PR fixes all 3 of these issues by correcting the math for the existing half-pixel offsets. We can't remove the half-pixel offsets; otherwise, we'd re-open issues like this: We could optionally apply this half-pixel offsets to other |
|
@Ne0nWinds I see, thanks for the further explanation and the videos. I use those functions a lot in many of my tools and I worried that change could break something (never had problems on my specific use cases). Still, I'd would prefer to avoid a new costly function to address that specific use case. Could it be addressed in a simpler way using less operations or at least avoiding the |
|
I looked into this a bit more. I found a solution that's a bit faster and is more mathematically correct. I removed the call to My new proposed solution would be something like this: // Compute upper-left 2x2 of M*T
float ax = m.m0*t.m0 + m.m4*t.m1;
float ay = m.m1*t.m0 + m.m5*t.m1;
float bx = m.m0*t.m4 + m.m4*t.m5;
float by = m.m1*t.m4 + m.m5*t.m5;
// Map screen-space pixel-diamond half-axes (0.5, 0) and (0, 0.5) to world space via 2x2 inverse
float invDet = 0.5f/fabsf(ax*by - ay*bx);
float xOffset = fmaxf(fabsf(bx), fabsf(by))*invDet;
float yOffset = fmaxf(fabsf(ax), fabsf(ay))*invDet;This does two things:
The OpenGL spec says that a line should be rasterized by testing if it exits an implicit diamond shape within a pixel, which is given by About the performance:
@raysan5 Would this approach work for you? If so, I'll update the PR with it |
@Ne0nWinds thanks for the further review and improvement, new proposed solution looks way better to me but please, could we avoid the additional function and just add that code to |
|
@Ne0nWinds thank you veery much for the improvement and the review! |
|
Wow, you merged that super fast! I was going to post some additional info: I made sure the ternary optimizes down to a few cheap CPU instructions: https://godbolt.org/z/GTTKdM4bE Also, pr_rotate_demo.mp4Also, there have been many reported issues for drawing lines in the past, some of which never quite got resolved. Examples:
Applying a similar fix to the It's up to you if the extra math in the |
|
@Ne0nWinds thanks for additional info, great to know it's so optimized. yeah, there have been so many attempts to address that pixel offset that I'm quite skeptic but we can try one last time and see if it works (still, I think it could be driver/GPU-dependant). What are the |
|
I think these four functions would be affected:
These two are not affected because they don't render with
The easiest one to start with would be I also think we actually may be able to simplify the math a bit further for these functions too. They don't have the same concerns that |

DrawRectangleLinesused the diagonal entries of the transform matrix to compute half-pixel offsets.There were two main problems with this approach:
BeginMode2D.Repro: Push/Pop Matrices
Using
rlRotatefto rotate the rectangle by90degrees causes the rectangle to become invisible. All rotation matrices cause the offsets to be incorrect, but this is the most obvious example.Repro: BeginMode2D with a camera
The half-pixel offsets completely ignore the
modelviewmatrix. This is most obvious when rendering withBeginMode2D(camera). Simple example:In the example above, the
DrawRectangledraws a significantly larger rectangle thanDrawRectangleLines!Solution
rlGetMatrixTransform) with the modelview matrix (rlGetMatrixModelview). We only need the upper-left 2x2 section of the matrix for 2D lines.